package com.app.twth.ui.lpnphoto.lpn;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.app.Constant;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityLpnphotoLpnBinding;
import com.app.twth.ui.lpnphoto.submit.LPNPhotoSubmitActivity;
import com.app.twth.ui.tally.Tally;
import com.app.twth.utils.EditTextUtil;
import com.app.twth.utils.ToastUtil;
import com.app.twth.utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class LPNPhotoLPNActivity extends BaseActivity<ActivityLpnphotoLpnBinding, LPNPhotoLPNViewModel> {
    private String TAG = getClass().getSimpleName();

    private int type;
    private Tally tally;
    private LPNPhotoLPNAdapter adapter;
    private List<String> tallyList = new ArrayList<>();


    @Override
    protected void initListener() {
        viewModel.getPutawayDetail(tally.getOrderId());
        viewModel.tallyDetailsLiveData.observe(this, data -> {
            tallyList.clear();
            tallyList.addAll(data);
            adapter.setData(data, type);
        });
        viewModel.photoInfoLiveData.observe(this, data -> {
            Bundle bundle = new Bundle();
            data.setContainerNo(tally.getContainerNo());
            bundle.putSerializable("PhotoInfo", data);
            AppRouter.showActivityForResult(mContext, LPNPhotoSubmitActivity.class, bundle, Constant.ForResult2);
        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    @Override
    protected String setTitle() {
        return "LPNPhoto(LPN)";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setNext("");
        Bundle bundle = getIntent().getBundleExtra(AppRouter.K_EXTRA);
        tally = (Tally) bundle.getSerializable("Tally");

        binding.tvContainerNo.setText(tally.getContainerNo());
        binding.tvCode.requestFocus();

        adapter = new LPNPhotoLPNAdapter(this, tallyList, position -> {
            if (tallyList.size() < position) {
                return;
            }
            barcodeStr = tallyList.get(position);
            viewModel.getLpnPhotoInfo(tally.getOrderId(), barcodeStr);
        });
        binding.recyclerview.setHasFixedSize(true);
        binding.recyclerview.setItemAnimator(new DefaultItemAnimator());

        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerview.setLayoutManager(layoutManager1);
        //添加装饰类
//        binding.recyclerview.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST));
        binding.recyclerview.setAdapter(adapter);

        binding.tvCode.setOnEditorActionListener(EnterListenter);
    }


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_scan:
                setScan();
                break;
            case R.id.tv_reset:
                binding.tvCode.setText("");
                bottomLayoutBinding.tvHint.setText("");
                break;
            case R.id.tv_next:
                barcodeStr = binding.tvCode.getText().toString();
                if (Utils.isEmpty(barcodeStr)) {
                    ToastUtil.showToast(getString(R.string.scan_inbound_lpn_lower));
                    return;
                }
                viewModel.getLpnPhotoInfo(tally.getOrderId(), barcodeStr);
                break;
        }
    }

    private TextView.OnEditorActionListener EnterListenter = new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                EditTextUtil.hideInput(mContext);
                bottomLayoutBinding.tvHint.setText("");
                barcodeStr = binding.tvCode.getText().toString();
                viewModel.getLpnPhotoInfo(tally.getOrderId(), barcodeStr);
            }
            return false;
        }
    };

    @Override
    public void getScanninCode() {
        super.getScanninCode();
        bottomLayoutBinding.tvHint.setText("");
        binding.tvCode.setText(barcodeStr);
        viewModel.getLpnPhotoInfo(tally.getOrderId(), barcodeStr);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.ForResult2 && resultCode == RESULT_OK) {
            binding.tvCode.setText("");
            viewModel.getPutawayDetail(tally.getOrderId());
        }
    }
}
